using System;
using System.Reflection;

namespace ActorSystem
{
	//Class to match two classes
	//Use reflection to get the fields and compare them
	//null is emtpy eg. match anything
	class Matcher
	{
		public bool Match(Object data, Object template)
		{
			//Get the fields from a and b
			//If the fields is not the same then return false
			//eg. not the same amount of fields or something like that
			//else match field by field
			Type ta = data.GetType();
			Type tb = template.GetType();
			//If type mismatch return false
			if (!ta.Equals(tb))
			{
				return false;
			}
			FieldInfo[] fa = ta.GetFields();
			FieldInfo[] fb = tb.GetFields();
			//If diffenret length of array, return false
			if (fa.Length != fb.Length) 
			{
				return false;
			}
			int pointer = 0;
			FieldInfo temp = null;
			foreach(FieldInfo field in fa)
			{
				temp = fb[pointer++];
				//If type mismath return false
				if (!field.GetType().Equals(temp.GetType()))
				{
					return false;
				}
				//If values is not (equal or template is null) return false;
				if (!(field.GetValue(field).Equals(temp.GetValue(temp)) || temp.GetValue(temp).Equals(null)))
				{
					return false;
				}
			}
			
			//We have a match return true
			return true;
		}
	}
}

